const http = require("http");
const server = http.createServer();
http
  .get("http://nodejs.org/dist/index.json", (res) => {
    res.setEncoding("utf8");
    let rawData = "";
    res.on("data", (chunk) => {
      rawData += chunk;
    });
    res.on("end", () => {
      try {
        const parsedData = JSON.parse(rawData);
        console.log(parsedData);
        server.on("request", (req, res) => {
          res.end(JSON.stringify(parsedData));
        });
      } catch (e) {
        console.error(e.message);
      }
    });
  })
  .on("error", (e) => {
    console.error(`Got error: ${e.message}`);
  });

// server.on("request", (req, res) => {
//   res.end(JSON.stringify(parsedData));
// });
server.listen(3000, () => {
  console.log("server is running");
});
